//  Inside Microsoft Exchange 2007 Web Services
//  Appendix B
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CreateRecurringSeriesUsingTimeZoneLookup
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            ItemIdType itemId = CreateRecurringSeriesUsingTimeZoneLookup(binding);
        }

        /// <summary>
        /// Create a daily recurring series using the time zone of the local
        /// operating system. (Listing B-20)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <returns>Id of newly created recurring master for the series</returns>
        /// 
        public static ItemIdType CreateRecurringSeriesUsingTimeZoneLookup(
            ExchangeServiceBinding binding)
        {
            DateTime nov2nd20078amUnspec = new DateTime(
                2007, 11, 02, 08, 00, 00, DateTimeKind.Unspecified);
            DateTime twoAM = new DateTime(
                DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day, 02, 00, 00, DateTimeKind.Unspecified);

            // Basic new calendar item information
            //
            CalendarItemType newRecurringMaster = new CalendarItemType();
            newRecurringMaster.Start = nov2nd20078amUnspec;
            newRecurringMaster.StartSpecified = true;
            newRecurringMaster.End = nov2nd20078amUnspec.AddHours(1);
            newRecurringMaster.EndSpecified = true;
            newRecurringMaster.Subject = "Four Day Recurring Series in" +
                "our local time zone";

            // Recurrence information for the new series
            // 
            DailyRecurrencePatternType recurrencePattern =
                new DailyRecurrencePatternType();
            recurrencePattern.Interval = 1;
            NumberedRecurrenceRangeType recurrenceRange =
                new NumberedRecurrenceRangeType();
            recurrenceRange.StartDate = nov2nd20078amUnspec;
            recurrenceRange.NumberOfOccurrences = 4;

            // Assign the Time Zone of the current OS to our series
            //
            newRecurringMaster.MeetingTimeZone =
                TimeZoneHelpers.CreateTimeZoneTypeFromRegistryInformation(
                    TimeZoneHelpers.GetCurrentOSTimeZone());

            // Set the Recurring information on our new series
            //
            newRecurringMaster.Recurrence = new RecurrenceType();
            newRecurringMaster.Recurrence.Item = recurrencePattern;
            newRecurringMaster.Recurrence.Item1 = recurrenceRange;

            // Make our request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.SendMeetingInvitations =
                CalendarItemCreateOrDeleteOperationType.SendToNone;
            createItemRequest.SendMeetingInvitationsSpecified = true;

            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new CalendarItemType[1];
            createItemRequest.Items.Items[0] = newRecurringMaster;

            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("CreateCalendarItem failed with response code " +
                         responseMessage.ResponseCode.ToString());
            }
            return responseMessage.Items.Items[0].ItemId;
        }

    }
}
